****************************************************************************************************
****************************************************************************************************
*Replication file for:
*The Importance of a Liberal Power's Attention to Democratic Elections Around the World
*Johannes Bubeck Ashrakat Elshehawy Nikolay Marinov Federico Nanni
****************************************************************************************************
****************************************************************************************************


*This file does the following:

***		Take Nelda

***		Generate US Election dates

***		Merge with different data sources that are needed for the analysis

***		Generate BIAS Nelda, and save data for further analysis







*please change to your working directory
cd "/Data"


estimates drop _all
drop _all

*make sure the dataset is in the working directory above
use nelda

* some renaming (for Ragic)

local agrp "nelda1wereregularelectionssuspen nelda2werethesethefirstmultipart nelda3wasoppositionallowed 	nelda4wasmorethanonepartylegal nelda5wasthereachoiceofcandidate nelda6ifregular44weretheseelecti nelda7beforeelections44werethere nelda8didtheincumbentreachtheirt nelda9hadtheincumbentextendedhis nelda10wasthecountryruledbytrans nelda11beforeelections44arethere nelda12wastheincumbentorrulingpa nelda13wereoppositionleadersprev nelda14didsomeoppositionleadersb nelda15isthereevidencethatthegov nelda16intherunuptotheelection44 nelda17iseconomicgrowthinthecoun nelda18isthecountrysaidtobeinane nelda19isthecountrysaidtobealarg nelda20wastheofficeoftheincumben nelda21didtheincumbentrun nelda22ifnonelda21wasthereachose nelda23ifyesnelda22didasuccessor nelda24didtheincumbentspartylose nelda25weretherereliablepollstha nelda26ifyesnelda25weretheyfavor nelda27wasthevotecountagainforth nelda28isthereevidencethatreport nelda29werethereriotsandprotests nelda30ifyesnelda29didtheyinvolv nelda31ifyesnelda29didthegovernm nelda32wereresultsthatdidnotfavo nelda33wastheresignificantviolen nelda34wereresultsthatwerefavora nelda35ifyesneldda34wasthisinpar nelda36ifyesnelda34wasthisinpart nelda37ifyesnelda34wasanewelecti nelda38ifyesnelda37didvictorygot nelda39wastheincumbentreplaced nelda40ifyesnelda39didtheleaders nelda41ifyesnelda39wastheleaderr nelda42ifyesnelda39wasthereacoup nelda43whatwasthenameoftheleader nelda44whatwasthenameoftheleader nelda45wereinternationalmonitors nelda46ifyesnelda4544werewestern nelda47ifyesnelda4644werethereal nelda48weresomemonitorsdeniedthe nelda49didanymonitorsrefusetogot nelda50isthecountrysaidtobeingoo nelda51isthereanegativechangeinr nelda52isthereapositivechangeinr nelda53isthecountrysaidtohaveasu nelda54ifyesnelda5344whichone nelda55isthereanegativechangeint nelda56isthereapositivechangeint nelda57isaidcutoff44orthreatened nelda58didanoutsideactorattemptt"

local bgrp "nelda1 nelda2 nelda3 nelda4 nelda5 nelda6 nelda7 nelda8 nelda9 nelda10 nelda11 nelda12 nelda13 nelda14 nelda15 nelda16 nelda17 nelda18 nelda19 nelda20 nelda21 nelda22 nelda23 nelda24 nelda25 nelda26 nelda27 nelda28 nelda29 nelda30 nelda31 nelda32 nelda33 nelda34 nelda35 nelda36 nelda37 nelda38 nelda39 nelda40 nelda41 nelda42 nelda43 nelda44 nelda45 nelda46 nelda47 nelda48 nelda49 nelda50 nelda51 nelda52 nelda53 nelda54 nelda55 nelda56 nelda57 nelda58"

local n : word count `agrp'

forvalues i = 1/`n' {
    local a : word `i' of `agrp'
    local b : word `i' of `bgrp'
    capture rename `a' `b'
    }
	
*******
*
*******


*** ELECTIONID


replace electionid=trim(electionid)
replace electionid="0"+electionid if length(electionid)==15
assert length(electionid)==16


**** ROUND
gen round=int(real(substr(electionid, 16, 1)))


***	
*
*	USING DATES	
*
***

*** Date (NELDA into STATA dates):

gen mmdd_s=string(mmdd)
replace mmdd_s="0" + mmdd_s if length(mmdd_s)==3
gen year_s=string(year)
drop if (length(mmdd_s)!=4 | length(year_s)!=4)

gen date_str= mmdd_s+"/"+year_s
gen date=date(date_str,"MDY")
format date %td

drop date_str year_s

*	Label for what follows

label define prox 0 "Out" 1 "In"

********** 
*
* 	This generates Proximity to Pres and Midterm ones:
*	
*
**********

* Proximity US Prez election 

gen uspreselein15=.
gen uspreselein3=.
gen uspreselein6=.


label var uspreselein15 "Pres Ele 1.5 mos"
label var uspreselein3 "Pres Ele 3 mos"
label var uspreselein6 "Pres Ele 6 mos"

label val uspreselein15 prox
label val uspreselein3 prox
label val uspreselein6 prox

***For Fig D8 - #months away from a US Presidential election

gen uspreselein1=.
gen uspreselein2=.
gen uspreselein4=.
gen uspreselein5=.
gen uspreselein7=.
gen uspreselein8=.
gen uspreselein9=.
gen uspreselein10=.
gen uspreselein11=.
gen uspreselein12=.
gen uspreselein13=.
gen uspreselein14=.
gen uspreselein15v1=.
gen uspreselein16=.
gen uspreselein17=.
gen uspreselein18=.



foreach i in "11,02,1948" "11,04,1952" "11,06,1956" "11,08,1960" "11,03,1964" "11,05,1968" "11,07,1972" "11,02,1976" "11,04,1980" "11,06,1984" "11,08,1988" "11,03,1992" "11,05,1996" "11,07,2000" "11,02,2004" "11,04,2008" "11,06,2012" "11,08,2016" "11,03,2020" { 

	replace uspreselein15=1  if inrange(date, mdy(`i')-45, mdy(`i')+45) & ccode!=2	
	replace uspreselein3=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2
	replace uspreselein6=1  if inrange(date, mdy(`i')-180, mdy(`i')+180) & ccode!=2
	
	
    replace	uspreselein1=1 if inrange(date, mdy(`i')-30, mdy(`i')+30) & ccode!=2
	replace uspreselein2=1 if inrange(date, mdy(`i')-60, mdy(`i')+60) & ccode!=2
	replace uspreselein4=1 if inrange(date, mdy(`i')-120, mdy(`i')+120) & ccode!=2
	replace uspreselein5=1 if inrange(date, mdy(`i')-150, mdy(`i')+150) & ccode!=2
	replace uspreselein7=1 if inrange(date, mdy(`i')-210, mdy(`i')+210) & ccode!=2
	replace uspreselein8=1 if inrange(date, mdy(`i')-240, mdy(`i')+240) & ccode!=2
	replace	uspreselein9=1 if inrange(date, mdy(`i')-270, mdy(`i')+270) & ccode!=2
	replace uspreselein10=1 if inrange(date, mdy(`i')-300, mdy(`i')+300) & ccode!=2
	replace uspreselein11=1 if inrange(date, mdy(`i')-330, mdy(`i')+330) & ccode!=2
	replace	uspreselein12=1 if inrange(date, mdy(`i')-360, mdy(`i')+360) & ccode!=2
	replace	uspreselein13=1 if inrange(date, mdy(`i')-390, mdy(`i')+390) & ccode!=2
	replace	uspreselein14=1 if inrange(date, mdy(`i')-420, mdy(`i')+420) & ccode!=2
	replace uspreselein15v1=1 if inrange(date, mdy(`i')-450, mdy(`i')+450) & ccode!=2
	replace uspreselein16=1 if inrange(date, mdy(`i')-480, mdy(`i')+480) & ccode!=2
	replace uspreselein17=1 if inrange(date, mdy(`i')-510, mdy(`i')+510) & ccode!=2
	replace	uspreselein18=1 if inrange(date, mdy(`i')-540, mdy(`i')+540) & ccode!=2

}

replace uspreselein15=0 if uspreselein15==. & ccode!=2
replace uspreselein3=0 if uspreselein3==. & ccode!=2
replace uspreselein6=0 if uspreselein6==. & ccode!=2


replace	uspreselein1=0 if uspreselein1==. & ccode!=2
replace uspreselein2=0 if uspreselein2==. & ccode!=2
replace uspreselein4=0 if uspreselein4==. & ccode!=2
replace uspreselein5=0 if uspreselein5==. & ccode!=2
replace uspreselein7=0 if uspreselein7==. & ccode!=2
replace uspreselein8=0 if uspreselein8==. & ccode!=2
replace	uspreselein9=0 if uspreselein9==. & ccode!=2
replace uspreselein10=0 if uspreselein10==. & ccode!=2
replace uspreselein11=0 if uspreselein11==. & ccode!=2
replace	uspreselein12=0 if uspreselein12==. & ccode!=2
replace	uspreselein13=0 if uspreselein13==. & ccode!=2
replace	uspreselein14=0 if uspreselein14==. & ccode!=2
replace uspreselein15v1=0 if uspreselein15v1==. & ccode!=2
replace uspreselein16=0 if uspreselein16==. & ccode!=2
replace uspreselein17=0 if uspreselein17==. & ccode!=2
replace	uspreselein18=0 if uspreselein18==. & ccode!=2

***generate the pre election window


gen uspreselein15_1sided=.
gen uspreselein3_1sided=.
gen uspreselein6_1sided=.

label var uspreselein15_1sided "Pres Ele 1.5 mos 1 sided"
label var uspreselein3_1sided "Pres Ele 3 mos 1 sided"
label var uspreselein6_1sided "Pres Ele 6 mos 1 sided"

label val uspreselein15_1sided prox2
label val uspreselein3_1sided prox2
label val uspreselein6_1sided prox2


foreach i in "11,02,1948" "11,04,1952" "11,06,1956" "11,08,1960" "11,03,1964" "11,05,1968" "11,07,1972" "11,02,1976" "11,04,1980" "11,06,1984" "11,08,1988" "11,03,1992" "11,05,1996" "11,07,2000" "11,02,2004" "11,04,2008" "11,06,2012" "11,08,2016" "11,03,2020" { 

	replace uspreselein15_1sided=1  if inrange(date, mdy(`i')-45, mdy(`i')+0) & ccode!=2	
	replace uspreselein3_1sided=1  if inrange(date, mdy(`i')-90, mdy(`i')+0) & ccode!=2
	replace uspreselein6_1sided=1  if inrange(date, mdy(`i')-180, mdy(`i')+0) & ccode!=2

}

replace uspreselein15_1sided=0 if uspreselein15_1sided==. & ccode!=2
replace uspreselein3_1sided=0 if uspreselein3_1sided==. & ccode!=2
replace uspreselein6_1sided=0 if uspreselein6_1sided==. & ccode!=2





***Bandwidth choice - generate decades



gen decade=1 if year>=1940 & year<1950
replace decade=2 if year>=1950 & year<1960
replace decade=3 if year>=1960 & year<1970
replace decade=4 if year>=1970 & year<1980
replace decade=2 if year>=1980 & year<1990
replace decade=2 if year>=1990 & year<2000
replace decade=2 if year>=2000 & year<2010
replace decade=2 if year>=2010 & year<2020





* MIDTERMS

capture drop uselein15 uselein3 uselein6

* first we simply assign values of presidential

gen uselein15=uspreselein15 
gen uselein3=uspreselein3 
gen uselein6=uspreselein6 

label var uselein15 "Ele 1.5 mos"
label var uselein3 "Ele 3 mos"
label var uselein6 "Ele 6 mos"

label val uselein15 prox
label val uselein3 prox
label val uselein6 prox

* here we add midterms

foreach i in "11,05,1946" "11,07,1950" "11,02,1954" "11,04,1958" "11,06,1962" "11,08,1966" "11,03,1970" "11,05,1974" "11,07,1978" "11,02,1982" "11,04,1986" "11,06,1990" "11,08,1994" "11,03,1998" "11,05,2002" "11,07,2006" "11,02,2010" "11,04,2014" "11,06,2018" "11,08,2022" { 

	replace uselein15=1  if inrange(date, mdy(`i')-45, mdy(`i')+45) & ccode!=2
	replace uselein3=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2
	replace uselein6=1  if inrange(date, mdy(`i')-180, mdy(`i')+180) & ccode!=2

}

replace uselein15=0 if uselein15==. & ccode!=2
replace uselein3=0 if uselein3==. & ccode!=2
replace uselein6=0 if uselein6==. & ccode!=2




******pre election window

gen uselein15_1sided=uspreselein15_1sided 
gen uselein3_1sided=uspreselein3_1sided 
gen uselein6_1sided=uspreselein6_1sided 

label var uselein15_1sided "Ele 1.5 mos_1sided"
label var uselein3_1sided "Ele 3 mos_1sided"
label var uselein6_1sided "Ele 6 mos_1sided"

label val uselein15_1sided prox2
label val uselein3_1sided prox2
label val uselein6_1sided prox2

* here we add midterms

foreach i in "11,05,1946" "11,07,1950" "11,02,1954" "11,04,1958" "11,06,1962" "11,08,1966" "11,03,1970" "11,05,1974" "11,07,1978" "11,02,1982" "11,04,1986" "11,06,1990" "11,08,1994" "11,03,1998" "11,05,2002" "11,07,2006" "11,02,2010" "11,04,2014" "11,06,2018" "11,08,2022" { 

	replace uselein15_1sided=1  if inrange(date, mdy(`i')-45, mdy(`i')+0) & ccode!=2
	replace uselein3_1sided=1  if inrange(date, mdy(`i')-90, mdy(`i')+0) & ccode!=2
	replace uselein6_1sided=1  if inrange(date, mdy(`i')-180, mdy(`i')+0) & ccode!=2

}

replace uselein15_1sided=0 if uselein15_1sided==. & ccode!=2
replace uselein3_1sided=0 if uselein3_1sided==. & ccode!=2
replace uselein6_1sided=0 if uselein6_1sided==. & ccode!=2






*** TRANSITIONS - whether US inc runs for re-election or not

* Proximity US Prez transition 

capture drop uspreselein3t

gen uspreselein3t=.

label var uspreselein3t "Pres Ele 3 mos transition"

label val uspreselein3t prox

foreach i in "11,04,1952" "11,08,1960" "11,05,1968" "11,02,1976" "11,08,1988" "11,07,2000" "11,04,2008" "11,08,2016"  { 

	replace uspreselein3t=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2

}

replace uspreselein3t=0 if uspreselein3t==. & ccode!=2

* Proximity US Prez NONtransition 

capture drop uspreselein3nt

gen uspreselein3nt=.

label var uspreselein3nt "Pres Ele 3 mos notransition"

label val uspreselein3nt prox

foreach i in "11,02,1948" "11,06,1956" "11,03,1964" "11,07,1972" "11,04,1980" "11,06,1984" "11,03,1992" "11,05,1996" "11,02,2004" "11,06,2012" "11,03,2020" { 

	replace uspreselein3nt=1  if inrange(date, mdy(`i')-90, mdy(`i')+90) & ccode!=2

}

replace uspreselein3nt=0 if uspreselein3nt==. & ccode!=2

	

	*	V-Dem

*	v2elrgstry v2elvotbuy v2elirreg v2elintim v2elpeace v2elfrfair v2elboycot v2elfrcamp v2elrstrct

		rename ccode COWcode
		replace COWcode=255 if COWcode==260
		replace COWcode=345 if country=="Serbia"
		merge m:1 COWcode year using  "V-Dem-CY-Full+Others-v11.1.dta", keepusing(v2elrgstry v2elvotbuy v2elirreg v2elintim v2elpeace v2elfrfair v2elboycot v2elfrcamp v2elrstrct)  
		replace COWcode=260 if COWcode==255 
		replace COWcode=340 if COWcode==345 
		drop if _m==2
		rename COWcode ccode 
		drop _m


 		
sum v2elrgstry v2elvotbuy v2elirreg v2elintim v2elpeace v2elfrfair v2elboycot v2elfrcamp v2elrstrct	

*** Above is V-Dem Measure of Election Bias

*** Coup paper data  by Goemans and Marinov


		merge m:1 ccode year using  "goemansmarinovbjps_rep.dta", keepusing(gdpM wbpop wbgdp wbpoptot colfra oda ODAUNITED_STATES)  
		drop if _m==2
		drop _m

gen odausa= ODAUNITED_STATES/gdpM


gen lnodaus=log(1+odausa)



**** US Aid Gov Patrick Shea Dataset

		merge m:1 ccode year using  "aiddata"
		drop if _m==2
		drop _m


replace total_milaid_constant=0 if total_milaid_constant==.
replace total_econaid_constant=0 if total_econaid_constant==.
gen lntusmilaid=log(total_milaid_constant+1)
gen lntuseconaid=log(total_econaid_constant+1)



***	defense. def treaty w USA

		merge m:1 ccode year using  "atop5_usa.dta"
		drop if _m==2
		drop _m

	
		replace defense=0 if defense==. & year<=2018
	
	
	
	
****		ATOP 5 USA Def Alli

***	defense. def treaty w USA

		merge m:1 ccode year using  "atop5_usa.dta"
		drop if _m==2
		drop _m

	
		replace defense=0 if defense==. & year<=2018
	
	
****		Sullivan et al mil aid 1990-200sth


		merge m:1 ccode year using  "Dataset-Military-Aid-and-Recipient-State-Cooperation-091310.dta", keepusing(militaryaid economicaid gdp2000 logmilitaryaid logeconomicaid loggdp2000 milaiddep2 econaiddep2)
		drop if _m==2
		drop _m

		sum  logmilitaryaid


****	Heretics data


		merge m:1 ccode year using  "basic_data_frame_generatedDD.dta", keepusing(worldbank polity2 fhscl poseu negeu euchange uschange posus negus lngdpM2 unus3l dem_usc dem_usp p s h rr dd exports imports expdep impdep usbase agree3unl)
		drop if _m==2
		drop _m

		
sum unus3l


		
		
		
			
*****
*
* INDEX of BIAS including relevant variables:
*
*****


capture drop bias

gen bias1=strpos(lower(nelda11),"y")>0
gen bias2=strpos(lower(nelda12),"y")>0
gen bias3=strpos(lower(nelda13),"y")>0
gen bias4=strpos(lower(nelda15),"y")>0
gen bias5=strpos(lower(nelda16),"y")>0




*	here bias is a measure of all these questions are 1 - average 0(.2)1

egen bias=rmean(bias1 bias2 bias3 bias4 bias5)

*	here bias is a measure of all these questions are 1 - average 0(.2)1
	
gen changed=strpos(lower(nelda6),"y")>0

*	extended term in past

gen bias9=(strpos(lower(nelda9),"y")>0 | strpos(nelda9,"/")>0)


save neldaprep,replace

